const { DataTypes, sequelize } = require("../config/sequelizeCommon");

const checkTableExistsQuery = `
SELECT table_name
  FROM information_schema.tables
  WHERE table_schema = 'OrderingSystem' AND table_name = 'employees'
`;

(() => {
    try {
        const existingTables =  sequelize.query(checkTableExistsQuery, {
            type: sequelize.QueryTypes.SELECT,
        });

        const Employee =  sequelize.define("Employee", {
            id: {
                type: DataTypes.UUID,
                primaryKey: true,
                defaultValue: DataTypes.UUIDV4, // 使用 UUID v4 生成默认值
              },
            //创建时间
            time: {
                type: DataTypes.DATE,
                defaultValue: DataTypes.NOW,
                allowNull: false,
            },
            //姓名
            name: {
                type: DataTypes.STRING(50),
                allowNull: false,
            },
            //头像
            job_number: {
                type: DataTypes.STRING(255),
                allowNull: false,
            },
            //性别
            sex: {
                type: DataTypes.STRING(20),
                allowNull: false,
            },
            //职业
            position: {
                type: DataTypes.STRING(50),
                allowNull: false,
            },
            //是否离职
            workerstate: {
                type: DataTypes.BOOLEAN,
                defaultValue: true,
                allowNull: false,
                comment: "是否离职",
            },
        });

        if (existingTables.length === 0) {
            // 表不存在，创建表
            Employee.sync();
        }

        // 导出 Customer
        module.exports = { Employee };
    } catch (error) {
        console.error("Error checking or creating user table:", error);
    }
})();